package com.qyhl.shop.shop.gift.detail;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.shop.R;
import com.qyhl.shop.R2;
import com.qyhl.shop.view.AlignTextView;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.shop.ShopGiftDetailBean;
import com.qyhl.webtv.module_user.share.UserShareUtil;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SHOP_GIFT_DETAIL)
public class ShopGiftDetailActivity extends BaseActivity implements ShopGiftDetailContract.ShopGiftDetailView {
    @BindView(R2.id.cover)
    ImageView cover;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.price)
    TextView price;
    @BindView(R2.id.status)
    ImageView status;
    @BindView(R2.id.date)
    TextView date;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.code)
    TextView code;
    @BindView(R2.id.code_layout)
    LinearLayout codeLayout;
    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.gift_introduction)
    TextView giftIntroduction;
    @BindView(R2.id.order_num)
    AlignTextView orderNum;
    @BindView(R2.id.order_time)
    TextView orderTime;
    @BindView(R2.id.qr_code_layout)
    RelativeLayout qrCodeLayout;
    @BindView(R2.id.shop_name)
    TextView shopName;

    @Autowired(name = "id")
    int userGiftId;

    @Autowired(name = "isCancel")
    boolean isCancel;

    private ShopGiftDetailBean mBean;

    private ShopGiftDetailPresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.shop_activity_gift_detail;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new ShopGiftDetailPresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(false);

        mPresenter.getDetail(userGiftId);
    }

    @Override
    protected void setListener() {
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getDetail(userGiftId);
            }
        });

        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(userGiftId);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(ShopGiftDetailBean bean) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mBean = bean;

        if (bean.getShopId() == 0) {
            shopName.setText(bean.getOrganizer());
            shopName.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            shopName.setText(bean.getShopName());
            shopName.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(this, R.drawable.user_arrow_right_gray), null);
        }

        title.setText(bean.getName());

        Glide.with(ShopGiftDetailActivity.this)
                .load(bean.getCoverPicUrl())
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(cover);

        if (bean.getPrice() == 0) {
            price.setText("免费");
        } else {
            price.setText("¥" + bean.getPrice());
        }

        date.setText("使用时间：" + bean.getExchangeBeginDate() + " ~ " + bean.getExchangeEndDate());

        address.setText(bean.getAddress());

        code.setText(bean.getQrcode());

        if (isCancel) {
            qrCodeLayout.setVisibility(View.GONE);
        } else {
            qrCodeLayout.setVisibility(View.VISIBLE);
            if (bean.isExpire() || bean.isUse()) {
                code.setTextColor(getResources().getColor(R.color.shop_common_message));
                code.setBackgroundResource(R.color.global_background);
                UserShareUtil shareUtil = new UserShareUtil(this, 265, bean.getQrcode(), qrCode, true);
            } else {
                code.setTextColor(getResources().getColor(R.color.shop_common_red));
                code.setBackgroundResource(R.color.shop_coupon_code_bg_on);
                UserShareUtil shareUtil = new UserShareUtil(this, 265, bean.getQrcode(), qrCode);
            }
        }


        if (bean.isUse()) {
            status.setVisibility(View.VISIBLE);
            status.setImageResource(R.drawable.shop_gift_detail_status_used);
        } else if (bean.isExpire()) {
            status.setVisibility(View.VISIBLE);
            status.setImageResource(R.drawable.shop_gift_detail_status_overtime);
        } else {
            status.setVisibility(View.GONE);
        }

        giftIntroduction.setText(bean.getNote());

        orderNum.setText("订单编号：" + bean.getOrderNumber());

        orderTime.setText("提交时间：" + bean.getCreateTime());

    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        loadMask.setReloadButtonText("点击重试~");
    }

    @OnClick({R2.id.back_btn, R2.id.shop_name})
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.back_btn) {
            finish();
        } else if (viewId == R.id.shop_name) {
            if (mBean != null && mBean.getShopId() != 0) {
                ARouter.getInstance()
                        .build(ARouterPathConstant.SHOP_SHOP_DETAIL)
                        .withInt("shopId", mBean.getShopId())
                        .navigation();
            }
        }
    }
}
